#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <trackerrequest.h>
#include <QDir>
#include "connectionsmanager.h"
#include <QApplication>


namespace Ui {
    class MainWindow;
}


class LoggerObserver {
public:
    virtual void log(const QString& textToLog) = 0;
};

class MainWindow : public QMainWindow, public LoggerObserver {
    Q_OBJECT
public:
    MainWindow(QWidget *parent = 0);
    void log(const QString& text);
    ~MainWindow();

protected:
    void changeEvent(QEvent *e);

private slots:
    void showOpenTorrentFileDialog();
    void startDownloadingFile();
    void trackerRequestCompleted(QString);
    void trackerRequestError(QString);
    void error(QString);
    void downloadCompleted();
    void updateDownloadsTable(QMap<TorrentConnection*,QPair<int,int> >*);
    void updateTransferRate(double transferRate);
private:
    void openTorrentFile(const QString& fileName);
    void showErrorMessage(const QString& reason);
private:
    Ui::MainWindow *ui;
    TrackerRequest* trackerClient;
    ConnectionsManager* connectionsManager;
    QDir dirToDownload;
};

class GlobalLogger {
public:
    static GlobalLogger* getInstance() {
        if(NULL == instance)
        {
            instance = new GlobalLogger();
        }
        return instance;
    }

    void setObserver(LoggerObserver* observer) {
        m_observer = observer;
    }

    void log(const QString& textToLog) {
        m_observer->log(textToLog);
    }

private:
    GlobalLogger(){};
    static GlobalLogger* instance;
    LoggerObserver* m_observer;
};


#endif // MAINWINDOW_H
